#include <stdio.h>
#include <string.h>
#include <openssl/dsa.h>
#include <openssl/objects.h>
#include "dsa.h"
#include <openssl/pem.h>

static void test_DSA_sign_c(void)
{
    int siglen = 2;
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);

    int dsa_sign = DSA_sign_c(1, "sign_c", "sig", &siglen, dsa);
    printf("dsa_sign: %d\n", dsa_sign);

    DSA_free_c(dsa);
    printf("test_DSA_sign_c  ok!\n");
}

static void test_DSA_verify_c(void)
{
    int siglen = 2;
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);
    int size_dsa = DSA_size_c(dsa);
    int DSA_verifydsa = DSA_verify_c(1, "sign_c", 2, "sig", 3, dsa);
    printf("DSA_verifydsa: %d\n", DSA_verifydsa);

    DSA_free_c(dsa);
    printf("test_DSA_verify_c  ok!\n");
}

static void test_DSA_do_sign_c(void)
{
    int siglen = 2;
    DSA_SIG *dsa_sign = DSA_do_sign_c("rootdir", 2, "./rsa_test_pri.pem", 1);
    printf("test_DSA_do_sign_c  ok!\n");
}

static void test_DSA_do_verify_c(void)
{
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);
    int gene_key = DSA_generate_key_c(dsa);
    int write_bio_prikey = PEM_write_bio_DSAPrivateKey_c("./rsa_test_pri.pem", dsa, 1);
    int write_bio_pubkey = PEM_write_bio_DSAPublicKey_c("rsa_test_pub.pem", dsa, 1);
    DSA_SIG *s = DSA_do_sign_c("./dir", 2, "./rsa_test_pri.pem", 1);
    int DSA_do_verify_dsa = DSA_do_verify_c("dgst", 2, s, "rsa_test_pub.pem", 1);
    DSA_free_c(dsa);
    printf("test_DSA_do_verify_c  ok!\n");
}

static void test_DSA_print_fp_c(void)
{
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);
    int DSA_print_fp_dsa = DSA_print_fp_c(dsa);

    DSA_free_c(dsa);
    printf("test_DSA_print_fp_c  ok!\n");
}

static void test_PEM_write_bio_DSAPublicKey_DSAPrivateKey_c(void)
{
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);
    int gene_key = DSA_generate_key_c(dsa);
    int write_bio_prikey = PEM_write_bio_DSAPrivateKey_c("./rsa_test_pri.pem", dsa, 1);
    int write_bio_pubkey = PEM_write_bio_DSAPublicKey_c("rsa_test_pub.pem", dsa, 1);
    DSA_free_c(dsa);
    printf("test_PEM_write_bio_DSAPublicKey_DSAPrivateKey_c  ok!\n");
}

static void test_PEM_write_bio_DSAparams_c(void)
{
    DSA *dsa = DSA_new_c();

    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);
    int PEM_write_bio_DSAparams_dsa = PEM_write_bio_DSAparams_c("pub_key_fn", dsa, 1);

    DSA_free_c(dsa);
    printf("test_PEM_write_bio_DSAparams_c  ok!\n");
}

static void test_PEM_read_bio_DSAPrivateKey_c(void)
{
    DSA *dsa = PEM_read_bio_DSAPrivateKey_c("pub_key_fn", 1);
    printf("test_PEM_read_bio_DSAPrivateKey_c  ok!\n");
}

static void test_PEM_read_bio_DSA_PUBKEY_c(void)
{
    DSA *dsa = PEM_read_bio_DSA_PUBKEY_c("pub_key_fn", 1);
    printf("test_PEM_read_bio_DSA_PUBKEY_c  ok!\n");
}

static void test_sign_string_calloc_c(void)
{
    DSA *dsa = DSA_new_c();
    int para_dsa = DSA_generate_parameters_ex_c(dsa, 2);

    unsigned char* cs = sign_string_calloc_c(dsa);
    printf("sign_string:  %s\n", cs);
    DSA_free_c(dsa);
    printf("test_sign_string_calloc_c  ok!\n");
}

void dsa_setup_tests(void)
{
    printf("dsa_setup_tests begin: \n");
    test_DSA_sign_c();
    test_DSA_verify_c();
    test_DSA_do_sign_c();
    test_DSA_do_verify_c();
    test_DSA_print_fp_c();
    test_PEM_write_bio_DSAPublicKey_DSAPrivateKey_c();
    test_PEM_write_bio_DSAparams_c();
    test_PEM_read_bio_DSAPrivateKey_c();
    test_PEM_read_bio_DSA_PUBKEY_c();
    test_sign_string_calloc_c();

    printf("dsa_setup_tests over  ok!\n\n");

}
